import BaseService from './base';
import Sequelize from 'sequelize';
const Op = Sequelize.Op;

export default class SumsService extends BaseService {
  constructor({ sumsDao }) {
    super();
    this.sumsDao = sumsDao
  }

  async getSumsAll (sums) {
    const [err, list] = await this.sumsDao.getSumsAll({
      attributes: ['t_name', 't_type', 't_sumHttps', 't_virtual', 't_loadSearchFailed', 
        't_loadSearchClosed', 't_loadLogoFailed', 't_invalidLogo', 't_invalidIP', 't_invalidSearch', 
        't_success', 'startTime', 'updateTime'],
      where: {
        mark: {[Op.ne]: sums.mark}
      }
    });
    if (err) {
      return this.fail('获取列表失败', null, err);
    }
    return this.success('查询成功', list);
  }

  async getSumsList(sums) {
    const [err, count, rows] = await this.sumsDao.getSumsList({
      where: {
        t_name: sums.t_name === '' ? {
          [Op.ne]: null
        } : sums.t_name,
        mark: {[Op.ne]: sums.mark}
      },
      limit: sums.limit,
      offset: sums.limit * (sums.offset - 1),
      distinct: true
    });
    if (err) {
      return this.fail('获取列表失败', null, err);
    }
    return this.success('查询成功', count, rows);
  }

  async addSums(sums) {
    const [addErr] = await this.sumsDao.addSums(sums);
    if (addErr) {
      return this.fail('添加失败', null, err);
    }
    return this.success('添加成功')
  }

  async delSums(sums) {
    const [err, updateSums] = await this.sumsDao.update(sums, {
      where: {
        id: sums.id
      }
    });
    if (err) {
      return this.fail('删除失败');
    }
    return this.success('删除成功');
  }

  async updateSums(sums) {
    const [err, updateSums] = await this.sumsDao.update(sums, {
      where: {
        id: sums.id
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }
}